#include "BodyFactory.h"

using namespace cocos2d;
#define DENSITY 2
#define FRICTION 0.3
#define RESTITUTION 0.3

b2Body* BodyFactory::createBody(vector<vector<b2Vec2> > vertices, b2World* world, b2BodyType bodyType)
{
	//convert vector of vertices to array of vertices
	// prepare body def
	b2BodyDef bodyDef;
	bodyDef.type = bodyType;

	b2Body* body = world->CreateBody(&bodyDef);

	b2FixtureDef fixtureDef;
	fixtureDef.density = DENSITY;
	fixtureDef.friction = FRICTION;
	fixtureDef.restitution = RESTITUTION;
	
	vector<vector<b2Vec2> >::iterator it;
	for (it = vertices.begin(); it < vertices.end(); it++)
	{
		vector<b2Vec2> verticesIndex = *it;
		b2PolygonShape* polygonShape = new b2PolygonShape();
		polygonShape->Set(&verticesIndex[0], verticesIndex.size());

		fixtureDef.shape = polygonShape;

		body->CreateFixture(&fixtureDef);
	}

	
	return body;
}

b2Body* BodyFactory::createBody(b2PolygonShape* shape, b2World* world, b2BodyType bodyType)
{
	//convert vector of vertices to array of vertices

	// prepare body def
	b2BodyDef bodyDef;
	bodyDef.type = bodyType;

	b2Body* body = world->CreateBody(&bodyDef);

	b2FixtureDef fixtureDef;
	fixtureDef.density = DENSITY;
	fixtureDef.friction = FRICTION;
	fixtureDef.restitution = RESTITUTION;
	
	vector<vector<b2Vec2> >::iterator it;
	
	fixtureDef.shape = shape;

	body->CreateFixture(&fixtureDef);

	return body;
}
